<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="primary" @click="out">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toogle-button" @click="collapsee">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active = "activepath"
        >
        <!-- router属性	是否使用 vue-router 的模式，
        启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id" >
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i :class="icaonsObj[item.id]"></i>
              <!-- 一级菜单文字 -->
              <span class="name">{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + item1.path"
              v-for="(item1, index) in item.children"
              :key="index"
              @click="savePath('/' + item.path)"
            >
              <!-- 二级菜单图标 -->
              <i class="el-icon-menu"></i>
              <!-- 二级菜单文字 -->
              <span>{{ item1.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      activepath: '',
      menulist: [],
      icaonsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      iscollapse: false,
    }
  },
  created() {
    this.getlist()
    this.activepath = window.sessionStorage.getItem('activepath')
  },
  mounted() {},
  methods: {
    out() {
      this.$message.success('成功退出登录')
      this.$router.push('/login')
      window.sessionStorage.clear('token')
    },
    async getlist() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    collapsee() {
      this.iscollapse = !this.iscollapse
    },
    savePath(path) {
      console.log(path)
      window.sessionStorage.setItem('activepath', path)
      this.activepath = path
    },
    handleOpen() {},
    handleClose() {},
  },
}
</script>

<style scoped lang="less">
.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-main {
  background-color: #e9edf1;
}

.home-container {
  height: 100%;
}

.el-aside {
  background-color: #333744;
  .toggle-button {
    background-color: #4a5064;
    font-size: 12px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.name {
  margin: 10px;
}
.el-menu {
  border-right: 0;
}
.toogle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
